package com.marketingexpert.controller.personmgnt;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.config.SystemErrorCode;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.storemgnt.StoreService;
import com.marketingexpert.service.storemgnt.StoreServiceFactory;
import com.marketingexpert.service.usermgnt.UserService;
import com.marketingexpert.util.Resources;
import com.marketingexpert.util.WebUtils;

public class GetTaobaoUserPersonalController implements Controller {

	private String successView = null;
	
	private UserService userService = null;
	
	private StoreServiceFactory storeServiceFactory;
	
	private SystemErrorCode systemErrorCode;
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if(request.getSession().getAttribute("isAdmin") != null && ((Boolean)request.getSession().getAttribute("isAdmin")) == true) {
			return new ModelAndView(getSuccessView());
		} else {
		
		try {	
		if(request.getSession().getAttribute("taobaoSessionKey") == null) {
			request.getSession().removeAttribute("parentUserId");
			request.getSession().removeAttribute("taobaoSessionKey");
			throw new MarketingExpertException(systemErrorCode.getSystemSessionExpired(), Resources.getString("SystemSessionExpired"));
		}
		String sessionKey = (String) request.getSession().getAttribute("taobaoSessionKey");
		StoreService storeService = getStoreServiceFactory().getService(StoreServiceFactory.TAOBAO);
		
		com.taobao.api.domain.User user = (com.taobao.api.domain.User)storeService.getUser(sessionKey);
		
		request.setAttribute("taobaoUser", user);
		
		return new ModelAndView(getSuccessView());
		} catch(MarketingExpertException e) {	
			if(e.getErrCode().equals(systemErrorCode.getInvalidSession()) || e.getErrCode().equals(systemErrorCode.getTaobaoExpiredSession())) {
				request.getSession().removeAttribute("taobaoSessionKey");
				request.getSession().removeAttribute("parentUserId");
				request.getSession().setAttribute("sessionExpired", Resources.getString("InvalidSession.PersonalData"));
				response.sendRedirect(WebUtils.getBasepath(request) + "/pages/sessionExpiredTip.jsp");
				return null;
			}
			request.setAttribute("failed", e.getMessage());
			return new ModelAndView(getSuccessView());
		}
		}
	}

	public String getSuccessView() {
		return successView;
	}

	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public StoreServiceFactory getStoreServiceFactory() {
		return storeServiceFactory;
	}

	public void setStoreServiceFactory(StoreServiceFactory storeServiceFactory) {
		this.storeServiceFactory = storeServiceFactory;
	}

	public SystemErrorCode getSystemErrorCode() {
		return systemErrorCode;
	}

	public void setSystemErrorCode(SystemErrorCode systemErrorCode) {
		this.systemErrorCode = systemErrorCode;
	}
}
